public class shuzhijisuan {
    public static int Max(int[] nums){
        if(nums==null||nums.length==0)

        {
            System.out.println("数组不能为空！");
            return 0;
        }

        int currentMax = nums[0];
        int globalMax = nums[0];
        for(
                int i = 1;
                i<nums.length;i++)

        {
            currentMax = Math.max(nums[i], currentMax + nums[i]);
            if (currentMax > globalMax) {
                globalMax = currentMax;
            }
        }
        return globalMax;
    }


    public static void main(String[] args){
        //示例1
        int[] arr1={1,-2,3,5,-1};
        System.out.println("数组1的最大子数组和："+Max(arr1));

        //示例2
        int[] arr2={1,-2,3,-8,5,1};
        System.out.println("数组2的最大子数组和："+Max(arr2));

        //示例3
        int[] arr3={1,-2,3,-2,5,1};
        System.out.println("数组3的最大子数组和："+Max(arr3));

        //示例4：全负数数组
        int[] arr4={-5,-3,-1};
        System.out.println("全负数数组的最大子数组和："+Max(arr4));
    }
}